import OpenAi from "openai";
import dotenv from "dotenv";
import ReadLine from "readline";
dotenv.config();
const openai = new OpenAi({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: process.env.OPENAI_BASE_URL,
});

const rl = ReadLine.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const messages = [];

async function main() {
  try {
    while (true) {
      const userInput = await new Promise((resolve) => {
        rl.question("User: ", (answer) => resolve(answer));
      });
      if (userInput.toLowerCase() === "exit") {
        rl.close();
        return;
      }
      messages.push({ role: "user", content: userInput });
      const response = await openai.chat.completions.create({
        model: "deepseek-chat",
        messages
      });
      const answer = response.choices[0].message.content;
      messages.push({ role: "assistant", content: answer });
      console.log("\x1b[32m%s\x1b[0m", "Deepseek: " + answer);
    }
  } catch (error) {
    console.log(error);
    rl.close();
  }
}
main()